﻿using JunionRobotTaskManager.Repository;
using SqlSugar;

namespace JunionRobotTaskManager.Extensions
{
    public static class SugarIocExtend
    {
        private readonly static List<string> isNullAttributes = new List<string>() { "Mode", "Status", "MapName", "User", "Password", "Output", "Input", "VIO" };
        private readonly static List<string> isIgnoreAttributes = new List<string>() { "PathPoint", "User", "Password", "Sensor", "CurKey", "Output", "Input", "VIO", "Sensor" };
        public static IServiceCollection AddRepository(this IServiceCollection services)
        {
            Console.WriteLine("DataSource=" + Path.Combine(System.Environment.CurrentDirectory, "DataBase", "JnRobot.db"));
            services.AddSingleton<ISqlSugarClient>(s =>
            {
                SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
                {
                  
                    ConnectionString = "DataSource=" + Path.Combine(System.Environment.CurrentDirectory, "DataBase", "JnRobot.db"),
                    DbType = DbType.Sqlite,
                    InitKeyType = InitKeyType.Attribute,
                    IsAutoCloseConnection = true,
                    ConfigureExternalServices = new ConfigureExternalServices()
                    {
                        EntityService = (type, column) =>
                        {
                            if (isIgnoreAttributes.Contains(type.Name))
                            {
                                column.IsIgnore = true;
                            }
                            if (isNullAttributes.Contains(type.Name))
                            {
                                column.IsNullable = true;
                            }
                        }
                    }
                }); ;
                return sqlSugar;
            });
            services.AddScoped<IRobotRepository, RobotRepository>();
            services.AddScoped<IJobRepoitory, JobRepository>();
            services.AddScoped<ITransferPortRepository, TransferPortRepository>();
            services.AddScoped<IDockRepository, DockRepository>();
            services.AddScoped<IParkRepository, ParkRepository>();

            return services;
        }
    }
}
